﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using TelecomGis.Helper.Encry;
using TelecomGis.Framework.Control;

namespace TelecomGis.Helper.Control
{
    public class ContronllerBase : Controller
    {
        public MaHoa encry = new MaHoa();

        private UTContextgis utContextgis = null;
        public UTContextgis UtContextgis
        {
            get
            {
                if (utContextgis == null)
                {
                    utContextgis = Session[UTContextgis.SessionContextKeySC] as UTContextgis;
                    if (utContextgis == null)
                    {
                        utContextgis = new UTContextgis();
                        Session[UTContextgis.SessionContextKeySC] = utContextgis;
                    }
                }
                return utContextgis;
            }
            private set { utContextgis = value; }
        }

        public JsonResult JsonError(string message)
        {
            JsonResualt response = new JsonResualt() { Status = JsonResualt.OperationFailure, Message = message };
            return new JsonResult() { Data = response, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public JsonResult JsonSuccess(string redirectUrl, string message = null)
        {
            JsonResualt response = new JsonResualt();
            response.Status = JsonResualt.OperationSuccess;
            response.RedirectUrl = redirectUrl;
            response.Message = message;

            return new JsonResult() { Data = response, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public JsonResult JsonSuccessObject(string redirectUrl, object _entityObject = null , string message = null)
        {
            JsonResualt response = new JsonResualt();
            response.Status = JsonResualt.OperationSuccess;
            response.RedirectUrl = redirectUrl;
            response.Message = message;
            response._entity = _entityObject;

            return new JsonResult() { Data = response, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public bool IsValidEmailAddress(string s)
        {
            if (string.IsNullOrEmpty(s))
                return false;

            Regex re = new Regex(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (re.IsMatch(s))
                return true;
            else
                return false;

        }

        public bool IsNumber(string text)
        {
            try
            {
                Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
                return regex.IsMatch(text);
            }
            catch { return false; }
        }

        public bool IsLetterWithDiacritics(string c)
        {
            var s = c.Normalize(NormalizationForm.FormD);
            return (s.Length > 1) &&
                   char.IsLetter(s[0]) &&
                   s.Skip(1).All(c2 => CharUnicodeInfo.GetUnicodeCategory(c2) == UnicodeCategory.NonSpacingMark);
        }

        public T TryGetTempValue<T>(string key)
        {
            return TryGetTempValue<T>(key, false);
        }

        public T TryGetTempValue<T>(string key, bool remove)
        {
            T tempObject = default(T);
            Type objtype = typeof(T);

            if (string.IsNullOrWhiteSpace(key))
            {
                return default(T);
            }

            string lookupKey = typeof(T).ToString() + "_" + key;

            try
            {
                tempObject = (T)Session[lookupKey];
                if (remove)
                {
                    Session.Remove(lookupKey);
                }
            }
            catch
            {
                tempObject = default(T);
            }

            return tempObject;
        }

        public void SetTempValue<T>(T obj, string key)
        {
            if (obj == null || string.IsNullOrWhiteSpace(key))
            {
                return;
            }

            string lookupKey = typeof(T).ToString() + "_" + key;

            Session[lookupKey] = obj;
        }

        public string GetContentType(string fileName)
        {

            string contentType = "application/octetstream";
            string ext = System.IO.Path.GetExtension(fileName).ToLower();
            Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
            if (registryKey != null && registryKey.GetValue("Content Type") != null)
                contentType = registryKey.GetValue("Content Type").ToString();
            return contentType;

        }

        public string[] RemoveDuplicates(string[] inputArray)
        {
            return inputArray.Distinct().ToArray<string>();
        }
       
    }

    [Serializable]
    public class JsonResualt
    {
        [NonSerialized]
        public static readonly string OperationSuccess = "success";

        [NonSerialized]
        public static readonly string OperationFailure = "error";

        public string Status { get; set; }
        public string Message { get; set; }
        public string RedirectUrl { get; set; }
        public object _entity { get; set; }
    }

    [Serializable]
    public class JsonResualtObject
    {
        [NonSerialized]
        public static readonly string OperationSuccess = "success";

        [NonSerialized]
        public static readonly string OperationFailure = "error";

        public string Status { get; set; }
        public string Message { get; set; }
        public string RedirectUrl { get; set; }
        public object _entity { get; set; }
    }
}
